# -*- coding: UTF-8 -*-
from django.core import template
from django.models.blog import entrees

class LatestBlogEntriesNode(template.Node):
    def __init__(self, num, varname):
        self.num, self.varname = num, varname

    def render(self, context):
        context[self.varname] = entrees.get_list(limit=self.num)
        return ''

def do_get_latest_blog_entries(parser, token):
    """
    {% obt_dernieres_entrees_du_blog 2 as dernieres_entrees %}
    """
    bits = token.contents.split()
    if len(bits) != 4:
        raise template.TemplateSyntaxError, "La balise '%s' accepte trois arguments." % bits[0]
    if bits[2] != 'as':
        raise template.TemplateSyntaxError, "Le premier argument d'une balise '%s' doit être 'as'." % bits[0]
    return LatestBlogEntriesNode(bits[1], bits[3])

template.register_tag('obt_dernieres_entrees_du_blog', do_get_latest_blog_entries)